package com.ksu.manager.controller;

import java.io.File;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import org.springframework.web.servlet.ModelAndView;

import com.ksu.manager.model.ItemManager;
import com.ksu.manager.model.TypeItem;
import com.ksu.manager.model.pages.Pagination;
import com.ksu.manager.service.IItemManagerService;
import com.ksu.manager.service.ITypeItemService;
import com.ksu.manager.util.SysContext;


@Controller
@RequestMapping("/itemManager")
public class ItemManagerController {
	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(ItemManagerController.class);
	
	private IItemManagerService itemManagerService;
	private ITypeItemService typeItemService;

	
	public ITypeItemService getTypeItemService() {
		return typeItemService;
	}


	public void setTypeItemService(ITypeItemService typeItemService) {
		this.typeItemService = typeItemService;
	}


	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	public ModelAndView queryList(HttpServletRequest request, HttpServletResponse response, ItemManager itemManager, Pagination page) {
		ModelAndView mv = new ModelAndView("/itemManager/list");
		try {
			Map<String,Object> map = itemManagerService.queryList(itemManager,page);
			List<ItemManager> itemManagerList = null;
			if(map!=null && map.size()>0){
			   itemManagerList = (List<ItemManager>)map.get("list");
			   mv.addObject("page",map.get("page"));
			}
			List<TypeItem> typeItemList = typeItemService.queryList(new TypeItem());
			mv.addObject("itemManagerList", itemManagerList);
			mv.addObject("typeItemList", typeItemList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mv;
	}
	
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response,ItemManager itemManager){
		if(itemManager!=null){
			itemManagerService.add(itemManager);
		}
		Pagination page = new Pagination();
		page.init();
		return queryList(request,response,itemManager,page);
	}
	
	@RequestMapping(value = "/del")
	public ModelAndView del(HttpServletRequest request, HttpServletResponse response,ItemManager itemManager){
		if(itemManager!=null){
			itemManagerService.del(itemManager);
		}
		Pagination page = new Pagination();
		page.init();
		return queryList(request,response,itemManager,page);
	}
	

	@RequestMapping(value = "/preUpload")
	public ModelAndView preUpload(HttpServletRequest request, HttpServletResponse response,ItemManager itemManager){
		ModelAndView mv = new ModelAndView("/itemManager/upload");
		return mv;
	}
	
	@RequestMapping(value = "/upload")
	public ModelAndView upload(HttpServletRequest request,
			HttpServletResponse response, ItemManager itemManager) {
		try {
			// 转型为MultipartHttpRequest：       
	        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;    
			// 获得文件：       
	        MultipartFile file = multipartRequest.getFile("uploadExcel");          
	           
	        File excelFile = new File(SysContext.UPLOADPATH,"uploadExcel.xls"); 
	        if(excelFile.exists()){
	        	excelFile.delete();
	        	excelFile.createNewFile();
	        }
	        file.transferTo(excelFile);
			itemManagerService.loadExcel(excelFile);

		} catch (Exception e) {
			e.printStackTrace();
		}

		ModelAndView mv = new ModelAndView("/login/result");
		mv.addObject("result", "refreshParent");
		return mv;
	}


	public IItemManagerService getItemManagerService() {
		return itemManagerService;
	}


	public void setItemManagerService(IItemManagerService itemManagerService) {
		this.itemManagerService = itemManagerService;
	}
}
